<?php

namespace app\admin\controller;

use app\admin\model\AdminAdmin;
use app\admin\model\AdminPermission;
use Exception;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Validator as v;
use support\Request;
use support\Response;

class Admin_permission extends Base
{

    /**
     * 权限
     * @param  Request  $request
     * @return string|Response
     */
    public function index(Request $request)
    {
        if ($request->isAjax()) {
            $list = $this->model->order('id', 'desc')->select();
            return json(['msg'    => '', 'code' => 0, 'data' => $list->toArray(),
                         'extend' => ['count' => $list->count()],
            ]);
        }
        return view('admin_permission/index');
    }

    /**
     * 添加
     * @param  Request  $request
     * @return string|Response
     */
    public function add(Request $request)
    {
        if ($request->isAjax()) {
            $data = $request->post();
            //验证
            try {
                v::input($request->post(), [
                    'title' => v::length(1, 64)->noWhitespace()->setName('名称'),
                    'href'  => v::length(1, 64)->setName('路由'),
                    'type'  => v::length(1, 64)->setName('类型'),
                    'sort'  => v::Digit()->length(1, 99)->setName('排序'),
                ]);
            } catch (ValidationException $e) {
                return json(['code' => 201, 'msg' => '添加失败', 'data' => $e->getMessage()]);
            }
            try {
                $this->model->create($data);
            } catch (Exception $e) {
                return json(['code' => 201, 'msg' => '添加失败', 'data' => $e->getMessage()]);
            }
            (new AdminAdmin())->upMenu();
            return json(['code' => 200, 'msg' => '添加成功']);
        }
        return view('admin_permission/add', [
            'permissions' => get_tree($this->model->order('sort', 'asc')->select()->toArray()),
        ]);
    }

    /**
     * 编辑
     * @param  Request  $request
     * @return string|Response
     */
    public function edit(Request $request)
    {
        $id = $request->input('id');
        $model = $this->model->find($id);
        if ($request->isAjax()) {
            $data = $request->post();
            $data['id'] = $model['id'];
            //验证
            try {
                v::input($request->post(), [
                    'title' => v::length(1, 64)->setName('名称'),
                    'href'  => v::length(1, 64)->setName('路由'),
                    'type'  => v::length(1, 64)->setName('类型'),
                    'sort'  => v::Digit()->length(1, 99)->setName('排序'),
                ]);
            } catch (ValidationException $e) {
                return json(['code' => 201, 'msg' => '添加失败', 'data' => $e->getMessage()]);
            }
            try {
                $model->save($data);
            } catch (Exception $e) {
                return json(['code' => 201, 'msg' => '更新失败', 'data' => $e->getMessage()]);
            }
            (new AdminAdmin())->upMenu();
            return json(['code' => 200, 'msg' => '更新成功']);
        }
        return view('admin_permission/edit', [
            'model'       => $model,
            'permissions' => get_tree(($this->model->order('sort', 'asc'))->select()->toArray()),
        ]);
    }

    /**
     * 禁用，启用
     * @param  Request  $request
     */
    public function status(Request $request)
    {
        $id = $request->input('id');
        $model = $this->model->find($id);
        if ($model->isEmpty()) {
            return json(['code' => 201, 'msg' => '数据不存在']);
        }
        try {
            $model->status = $request->post('status');
            $model->save();
        } catch (Exception $e) {
            return json(['code' => 201, 'msg' => '更新失败', 'data' => $e->getMessage()]);
        }
        (new AdminAdmin())->upMenu();
        return json(['code' => 200, 'msg' => '更新成功']);
    }

    /**
     * 删除
     * @param  Request  $request
     */
    public function del(Request $request)
    {
        return json(['code' => 201, 'msg' => '演示禁止删除']);

        $id = $request->input('id');
        $model = $this->model->with('child')->find($id);
        if (isset($model->child) && !$model->child->isEmpty()) {
            return json(['code' => 201, 'msg' => '存在子权限，禁止删除']);
        }
        try {
            $model->delete();
        } catch (Exception $e) {
            return json(['code' => 201, 'msg' => '删除失败', 'data' => $e->getMessage()]);
        }
        (new AdminAdmin())->upMenu();
        return json(['code' => 200, 'msg' => '删除成功']);
    }

    protected function initialize()
    {
        $this->model = new AdminPermission;
    }
}
